use uuid::Uuid;
use rbatis::rbdc::DateTime;
use rbatis::Result;
use crate::config::init_db;
use serde::{Deserialize,Serialize};
use rbs::to_value;

pub struct ArticleTagRelation {
    pub id: Uuid,
    pub tag_id: Uuid,
    pub article_id: Uuid,
}

pub struct Article {
    pub id: Uuid,
    pub title: String,
    pub raw_content: String,
    pub content: String,
    pub published: bool,
    pub create_time: DateTime,
    pub modify_time: DateTime,
}

pub struct Comments {
    id: Uuid,
    comment: String,
    article_id: Uuid,
    user_id: Uuid,
    nickname: String,
    create_time: DateTime,
}

#[derive(Deserialize)]
pub struct Tags {
    id: Uuid,
    tag: String,
}

impl Tags {
    pub async fn view_list_tag() -> Result<Vec<Tags>> {
        let db = init_db();
        db.query_decode::<Vec<Self>>("SELECT * FROM tags",vec![]).await
    }
}
pub struct Users {
    pub id: Uuid,
    pub account: String,
    pub github: Option<String>,
    pub password: String,
    pub salt: String,
    pub groups: i16,
    pub nickname: String,
    pub say: Option<String>,
    pub email: String,
    pub disabled: i16,
    pub create_time: DateTime,
}

struct RawArticlesWithTag {
    pub id: Uuid,
    pub title: String,
    pub raw_content: String,
    pub content: String,
    pub published: bool,
    pub tags_id: Vec<Option<Uuid>>,
    pub tags: Vec<Option<String>>,
    pub create_time: DateTime,
    pub modify_time: DateTime,
}

#[derive(Deserialize, Serialize)]
pub struct ArticlesWithTag {
    pub id: Uuid,
    pub title: String,
    pub content: String,
    pub published: bool,
    pub tags_id: Option<Vec<Uuid>>,
    pub tags: Option<Vec<String>>,
    pub create_time: DateTime,
    pub modify_time: DateTime,
}

impl Article {
    pub async fn query_article(id: Uuid, admin: bool) -> Result<ArticlesWithTag> {
        let db = init_db();
        let a = db.query_decode("select * from article_with_tag where id = ?",vec![to_value!(id)]).await?;

    }
}